
package inf3791.calendarsystem.others.components;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 * Displays the current time.
 * @author Jonas Weber
 */
public class ClockWidget extends JLabel implements Runnable {
    private ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1);
    
    public ClockWidget()
    {
        executor.scheduleAtFixedRate(this, 0, 500, TimeUnit.MILLISECONDS);
    }

    final static DateFormat FORMATTER = SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT);
    
    static String calculateString()
    {
        return FORMATTER.format(new Date());
    }
    
    @Override
    public void run() {
        
        final String currenttext = calculateString();
        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                setText(currenttext);
            }
        });
    }
}
